/**
 * 
 */
package com.aspire.wap.core.io;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

/**
 * <p>Excel工具类</p>
 * @author sunquanzhi
 * @version 1.0
 * @deprecated
 */
@SuppressWarnings("unchecked")
public class ExcelUtil {

	/**
	 * <p>解析Excel文件，返回map对象</p>
	 * <p>MAP结构：sheet名称------sheet解析后的结构List(行集合)</p>
	 * <p>行结构：行List由每个表格值组成</p>
	 * @param filename 文件名
	 * @return 返回解析后的map结果集合
	 */
	public static Map<String,List> parser(String filename)
	{
		Map<String,List> result = new HashMap<String,List>();
		try{
			HSSFWorkbook workbook = new HSSFWorkbook ( new FileInputStream (filename ) ) ;
			for(int i = 0; i < workbook.getNumberOfSheets(); i++)
			{
				
				if(null != workbook.getSheetAt(i))
				{
					HSSFSheet hssfSheet = workbook.getSheetAt(i);
					List<List> sheetList = getSheet(hssfSheet);
					result.put(workbook.getSheetName(i),sheetList);
				}
				
			}
			
		}catch(Exception e)
		{
			e.printStackTrace();
		}
		return result;
	}
	/**
	 * @description 对一个sheet进行解析
	 * @param sheet 表单
	 * @return 返回表单list
	 */
	private static List<List> getSheet(HSSFSheet sheet)
	{
		
		List<List> list = new ArrayList<List>();
		try{
			for(int i=sheet.getFirstRowNum();i<=sheet.getLastRowNum();i++)
			{
				if(null != sheet.getRow(i))
				{
					list.add(getRow(sheet.getRow(i)));
				}
			}
		}catch(Exception e)
		{
			e.printStackTrace();
		}
		return list;
	}
	/**
	 * @description 对单一条记录进行解析
	 * @param row
	 * @return 返回一行单元格内容列表
	 */
	private static List<String> getRow(HSSFRow row)
	{
		
		List<String> list = new ArrayList<String>();
		try{
			for(short i = row.getFirstCellNum();i <= row.getLastCellNum();i++)
			{
				
				if(null != row.getCell(i) )
				{
						HSSFCell cell = row.getCell(i);
						switch(cell.getCellType())
						{
						case HSSFCell.CELL_TYPE_NUMERIC:
							Double value = new Double(cell.getNumericCellValue());		
							list.add(i,String.valueOf(value));
							break;
						case HSSFCell.CELL_TYPE_STRING:							
							list.add(i,cell.getStringCellValue());
							break;
						case HSSFCell.CELL_TYPE_BLANK:
							list.add(i,cell.getStringCellValue());
							break;
						case HSSFCell.CELL_TYPE_BOOLEAN:
							list.add(i,String.valueOf(cell.getBooleanCellValue()));
							break;
						case HSSFCell.CELL_TYPE_ERROR:
							list.add(i,String.valueOf(cell.getErrorCellValue()));
							break;
						default:
							list.add(i,"");
							break;
						}
				}else{
					list.add(i,"");
				}
			}
			
		}catch(Exception e)
		{
			e.printStackTrace();
		}
		return list;
	}
	
	
	
	
	/**
	 * <p>根据MAP结构创建一个EXCEL文件</p>
	 * @param map
	 * @param filename
	 */
	public static void createXSL(Map map,String filename)
	{
		try{
			createXSL(map,new FileOutputStream(filename));
		}catch(Exception e)
		{
			e.printStackTrace();
		}
	}
	/**
	 * 创建一个EXCEL文件
	 * @param map
	 * @param os
	 */
	private static void createXSL(Map map,OutputStream os)
	{
		HSSFWorkbook wb = new HSSFWorkbook();
		
		Set set = map.keySet();
		Iterator iter = set.iterator();
		while(iter.hasNext())
		{
			String workbookName = (String)iter.next();
			List listValue = (List)map.get(workbookName);
			HSSFSheet sheet = wb.createSheet(workbookName);
			createSheet(sheet,listValue);
			
			
			
		}
		try{
			wb.write(os);
			os.close();
		}catch(Exception e)
		{
			e.printStackTrace();
		}
	}
	/**
	 * 创建一个表单元
	 * @param sheet
	 * @param value
	 */
	private static void createSheet(HSSFSheet sheet,List value)
	{
	
		for(int i=0;i<value.size();i++)
		{
			Object obj = value.get(i);
			if(obj != null)
			{
				List rowValue = (List)obj;
				HSSFRow hssfRow = sheet.createRow(i);
				createRow(hssfRow,rowValue);
				
			}
		}
	}
	/**
	 * 创建一行数据
	 * @param row
	 * @param value
	 */
	private static void createRow(HSSFRow row,List value)
	{
		for(short i=0;i<value.size();i++)
		{
			Object obj = value.get(i);
			if(obj !=null)
			{
				HSSFCell cell = row.createCell(i);
				//cell.setEncoding(HSSFCell.ENCODING_UTF_16);
				if(obj instanceof String[] )
				{
					String[] map = (String[])obj;
					int type = HSSFCell.CELL_TYPE_ERROR;
					try{
						type = Integer.parseInt(map[1]);
					}catch(Exception e){}
					cell.setCellType(type);
					switch(type)
					{
					case HSSFCell.CELL_TYPE_BLANK:
						break;
					case HSSFCell.CELL_TYPE_BOOLEAN:
						cell.setCellValue(new Boolean(map[0]));
						break;
					case HSSFCell.CELL_TYPE_ERROR:
						cell.setCellErrorValue(map[0].getBytes()[0]);
						break;
					case HSSFCell.CELL_TYPE_FORMULA:
						cell.setCellFormula(map[0]);
						break;
					case HSSFCell.CELL_TYPE_NUMERIC:
						cell.setCellValue(Double.parseDouble(map[0]));
						break;
					case HSSFCell.CELL_TYPE_STRING:
						cell.setCellValue(map[0]);
						break;
					default:
						cell.setCellValue(map[0]);
						break;
					}
					
				}else{
					cell.setCellType(HSSFCell.CELL_TYPE_STRING);
					cell.setCellValue((String)obj);
				}
				
				
			}
		}
	}
}
